import { createI18n } from 'vue-i18n'

// 获取浏览器语言
const getLanguage = () => {
  const savedLanguage = localStorage.getItem('language')
  if (savedLanguage) {
    return savedLanguage
  }
  
  const browserLanguage = navigator.language.toLowerCase()
  if (browserLanguage.includes('zh')) {
    return 'zh'
  }
  if (browserLanguage.includes('en')) {
    return 'en'
  }
  return 'tl'
}

const i18n = createI18n({
  legacy: false,
  locale:  'tl', /// getLanguage(), 注释缓存
  fallbackLocale: 'tl',
  messages: {
    tl: {},
    zh: {},
    en: {},
  },
})

export default i18n

// 导出切换语言的函数
export const switchLanguage = (lang: string) => {
  i18n.global.locale.value = lang as any
  localStorage.setItem('language', lang)
  
  // 更新HTML lang属性
  document.documentElement.lang = lang
} 